package activity;

import java.util.HashMap;
import java.util.Map;

import com.android.volley.VolleyError;
import com.qzhy.female.R;
import com.sina.weibo.sdk.auth.AuthInfo;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.sina.weibo.sdk.auth.sso.SsoHandler;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import Constant.Constance;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import model.Sign_status;
import utils.GsonUtils;
import utils.HTTPUtils;
import utils.SetUtils;
import volley.VolleyListener;


public class LoginActivity extends BaseActivity implements OnClickListener  {

	private ImageView mImgBack;
	private TextView mTvRegist;
	private RelativeLayout mRtlLogin;
	private EditText mEtuser;
	private EditText mEtPassword;
	private AuthInfo mAuthInfo;
	private SsoHandler mSsoHandler;
	private Oauth2AccessToken mAccessToken;
	private TextView tvToken;
	private ImageView mImgWb;
	private ImageView mImgWx;
	private ImageView mImgQQ;
	private UMShareAPI mShareAPI = null;//友盟分享
	private String UserType;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		mShareAPI = UMShareAPI.get( this );
		InitUI();	
		
	}
	private void InitUI() {
     mImgBack = (ImageView) findViewById(R.id.img_back);	
     mImgBack.setOnClickListener(this);
     mRtlLogin = (RelativeLayout) findViewById(R.id.rtl_login);
     mRtlLogin.setOnClickListener(this);
     
     mEtuser = (EditText) findViewById(R.id.et_phone);
     mEtPassword = (EditText) findViewById(R.id.et_password);
     
     tvToken = (TextView)findViewById(R.id.tv_token);
     
     mImgWb = (ImageView) findViewById(R.id.img_wb);
     mImgWb.setOnClickListener(this);
     mImgWx = (ImageView) findViewById(R.id.img_wx);
     mImgWx.setOnClickListener(this);
     
     mImgQQ = (ImageView) findViewById(R.id.img_qq);
     mImgQQ.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		SHARE_MEDIA platform = null;
		switch (v.getId()) {
		case R.id.img_back:
			finish();
			break;
		case R.id.rtl_login:	
			Login();
			break;
		case R.id.img_wb:
			 UserType=Constance.USERTYPE.XINLIANG;
			 platform = SHARE_MEDIA.SINA;
			 mShareAPI.doOauthVerify(LoginActivity.this, platform, umAuthListener);
			break;
		case R.id.img_wx://微信登陆
			UserType=Constance.USERTYPE.WEIXIN;
			platform = SHARE_MEDIA.WEIXIN;
			mShareAPI.doOauthVerify(LoginActivity.this, platform, umAuthListener);
			break;
		case R.id.img_qq:
			UserType=Constance.USERTYPE.QQ;
			platform = SHARE_MEDIA.QQ;
			mShareAPI.doOauthVerify(LoginActivity.this, platform, umAuthListener);
			break;
		default:
			break;
		}
	}
	private void Login() {
		String user = mEtuser.getText().toString().trim();
		String password = mEtPassword.getText().toString().trim();
		Map<String, String> params=new HashMap<String,String>();
		params.put("phone", user);
		params.put("password", password);
		HTTPUtils.post(getApplicationContext(), Constance.URL.USER_LOGIN, params, new VolleyListener() {
			
			@Override
			public void onResponse(String arg0) {
				Toast.makeText(getApplicationContext(), "返回用户登陆信息："+arg0.toString(), 100).show();
				Sign_status sign = GsonUtils.parseJSON(arg0, Sign_status.class);
				String status = sign.getStatus();
				if(status.equals("error")){
					Toast.makeText(getApplicationContext(), "登陆失败,请检查用用户名和密码!", 1).show();
				}else {
					SetUtils.saveLoginState(getApplicationContext(), true);
					SetUtils.saveUserName(getApplicationContext(), sign.getResult().getNickname());
					SetUtils.saveUserID(getApplicationContext(), sign.getResult().getId());
					if(sign.getResult().getProfileImg().startsWith("http")){
						SetUtils.saveUserIcon(getApplicationContext(), sign.getResult().getProfileImg());
					}else {
						SetUtils.saveUserIcon(getApplicationContext(), Constance.URL.IMG_BASE+sign.getResult().getProfileImg());
					}
					Intent data = new Intent();
					setResult(Constance.INTENT.LOGIN_NORMAL, data);
					finish();
				}
			}
			@Override
			public void onErrorResponse(VolleyError arg0) {
				Toast.makeText(getApplicationContext(), "用户登陆失败", 1).show();
			}
		});
	}
	
	/**
	 * 当 SSO 授权 Activity 退出时，该函数被调用。
	 * 
	 * **注意：必须重写这个方法，否则在手机中测试可能无法获得Token
	 * 
	 * @see {@link Activity#onActivityResult}
	 */
    private UMAuthListener umAuthListener = new UMAuthListener() {
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
            SetUtils.saveLoginState(getApplicationContext(), true);
            Intent intent=new Intent();
            switch (UserType) {
			case Constance.USERTYPE.XINLIANG:
				setResult(Constant.Constance.INTENT.lOGIN_XL, intent);
				Toast.makeText(getApplicationContext(), "新浪:Authorize succeed", Toast.LENGTH_SHORT).show();
				break;
			case Constance.USERTYPE.WEIXIN:
				setResult(Constant.Constance.INTENT.lOGIN_WX, intent);
				Toast.makeText(getApplicationContext(), "微信:Authorize succeed", Toast.LENGTH_SHORT).show();
				break;
			case Constance.USERTYPE.QQ:
				setResult(Constant.Constance.INTENT.lOGIN_QQ, intent);
				Toast.makeText(getApplicationContext(), "QQ:Authorize succeed", Toast.LENGTH_SHORT).show();
				break;
			default:
				break;
			}
			finish();
        }
		@Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            Toast.makeText( getApplicationContext(), "Authorize fail", Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            Toast.makeText( getApplicationContext(), "Authorize cancel", Toast.LENGTH_SHORT).show();
        }
    };
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mShareAPI.onActivityResult(requestCode, resultCode, data);
    }
    @Override
    protected void onDestroy() {
    	dismissLoadingDialog();
    	super.onDestroy();
    }
	
}
